<template>
	<view class="root-wrap">
		<view class="form-item">
			<view class="label">
				<text class="title">联系人</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入联系人"
					border="none"
					v-model="userInfo.contact_name"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'contact_name')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">联系人身份证号</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入联系人身份证号"
					border="none"
					v-model="userInfo.contact_idcard"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'contact_idcard')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">联系人手机</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入联系人手机"
					border="none"
					v-model="userInfo.contact_phone"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'contact_phone')"
				  ><template slot="suffix">
						<u-button
							color="transparent"
							type="primary"
							size="mini"
							open-type="getPhoneNumber"
							@getphonenumber="getphonenumber"
						><text class="bind">绑定电话</text></u-button>
					</template>
				</u-input>
			</view>
		</view>
		
		<view class="form-item">
			<view class="label">
				<text class="title">联系人邮箱</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入联系人邮箱"
					border="none"
					v-model="userInfo.contact_email"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'contact_email')"
				  ></u-input>
			</view>
		</view>
		<view class="submit"><u-button @click="onSubmit" color="#30D6C2" type="primary" shape="circle">保存<image class="icon" src="../../../static/assets/imgs/next-step.png"></image></u-button></view>
	</view>
</template>

<script>
	import { registerGetPhone,updateContactInfo,getUserInfo } from '@/api/user.js';
	export default {
		data() {
			return {
				userInfo: {},
				loading: false,
				readOnly: false,
			};
		},
		onLoad(){
			this.userInfo = this.vuex_user;
			this.getUserInfo();
		},
		methods:{
			getUserInfo(){
				getUserInfo().then((res)=>{
					if (1 == res.code) {
						this.userInfo = res.data;
						this.$u.vuex('vuex_user', res.data)
					} else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			change(e,key){
				this.userInfo[key] = e;
			},
			getphonenumber(res) {
				if (res.detail.errMsg == 'getPhoneNumber:fail user deny') {
					uni.showToast({
						title:'手机号获取失败',
						icon: 'none'
					})
				} else if ("getPhoneNumber:ok" == res.detail.errMsg) {
					console.log(res);
					registerGetPhone({
						encryptedData: res.detail.encryptedData,
						iv: res.detail.iv
					})
					.then(res => {
						console.log(res);
						this.userInfo.contact_phone = res.data.phone;
					})
				}
			},
			onSubmit(){
				if(this.loading) return;
				const { id,contact_name,contact_idcard,contact_phone,contact_email } = this.userInfo;
				
				if(!contact_name || !contact_idcard || !contact_phone || !contact_email){
					uni.showToast({
						title:'请检查所有必填项',
						icon:'none'
					})
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '提交中...'
				})
				const params = {
					"vendor_id": id, //供应商ID
					"contact_name": contact_name, //联系人
					"contact_idcard": contact_idcard, //联系人身份证
					"contact_phone": contact_phone, //联系人手机号
					"contact_email": contact_email //联系人邮箱
				}
				updateContactInfo(params).then(res => {
					this.loading = false;
					uni.hideLoading();
					if(res.code==1){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						const timer = setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
							clearTimeout(timer);
						},900);
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.root-wrap{
	padding-bottom: 110rpx;
	.form-item{
		margin: 0 24rpx;
		.label{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 44rpx 0 24rpx;
			.title{
				font-size: 28rpx;
				color: #606266;
				line-height: 50rpx;
			}
			.require{
				font-size: 28rpx;
				color: #E62020;
				line-height: 50rpx;
				margin-left: 24rpx;
			}
		}
		.input-wrap{
			width: 700rpx;
			height: 90rpx;
			background-color: #F3F4F6;
			margin: 0 auto;
			border-radius: 8rpx;
			.u-input{
				height: 100% !important;
				input{
					padding: 0 24rpx !important;
				}
			}
		}
	}
	.submit{
		width: 208rpx;
		height: 80rpx;
		margin: 88rpx auto 110rpx;
		.icon{
			width: 48rpx;
			height: 48rpx;
			margin-left: 16rpx;
		}
		.label{
			font-weight: 500;
			font-size: 32rpx;
			color: #fff;
		}
	}
	.u-input__content__subfix-icon{
		margin-right: 16rpx;
	}
	.bind{
		font-size: 28rpx;
		color: #909399;
	}
}
</style>
